/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jule.clases.treeFiles;

import java.awt.Component;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;
import jule.clases.field;

/**
 *
 * @author Administrador
 */
public class databaseTreeRenderer implements TreeCellRenderer {

    private DefaultTreeCellRenderer databaseIconRenderer;
    private DefaultTreeCellRenderer tableIconRenderer;
    private DefaultTreeCellRenderer columnIconRenderer;
    private DefaultTreeCellRenderer primaryKeyIconRenderer;
    private DefaultTreeCellRenderer foreignKeyIconRenderer;

    // tipos de datos...
    private DefaultTreeCellRenderer dateIconRenderer;

    public databaseTreeRenderer() {
        databaseIconRenderer = new DefaultTreeCellRenderer();
        databaseIconRenderer.setLeafIcon(new ImageIcon(getClass().getResource("/images/server_database.png")));
        databaseIconRenderer.setOpenIcon(new ImageIcon(getClass().getResource("/images/server_database.png")));
        databaseIconRenderer.setClosedIcon(new ImageIcon(getClass().getResource("/images/server_database.png")));

        tableIconRenderer = new DefaultTreeCellRenderer();
        tableIconRenderer.setLeafIcon(new ImageIcon(getClass().getResource("/images/table.png")));
        tableIconRenderer.setOpenIcon(new ImageIcon(getClass().getResource("/images/table.png")));
        tableIconRenderer.setClosedIcon(new ImageIcon(getClass().getResource("/images/table.png")));

        columnIconRenderer = new DefaultTreeCellRenderer();
        columnIconRenderer.setLeafIcon(new ImageIcon(getClass().getResource("/images/vcard.png")));
        columnIconRenderer.setOpenIcon(new ImageIcon(getClass().getResource("/images/vcard.png")));
        columnIconRenderer.setClosedIcon(new ImageIcon(getClass().getResource("/images/vcard.png")));

        primaryKeyIconRenderer = new DefaultTreeCellRenderer();
        primaryKeyIconRenderer.setLeafIcon(new ImageIcon(getClass().getResource("/images/table_key.png")));
        primaryKeyIconRenderer.setOpenIcon(new ImageIcon(getClass().getResource("/images/table_key.png")));
        primaryKeyIconRenderer.setClosedIcon(new ImageIcon(getClass().getResource("/images/table_key.png")));

        foreignKeyIconRenderer = new DefaultTreeCellRenderer();
        foreignKeyIconRenderer.setLeafIcon(new ImageIcon(getClass().getResource("/images/table_link.png")));
        foreignKeyIconRenderer.setOpenIcon(new ImageIcon(getClass().getResource("/images/table_link.png")));
        foreignKeyIconRenderer.setClosedIcon(new ImageIcon(getClass().getResource("/images/table_link.png")));

        // para los tipos de datos
         dateIconRenderer = new DefaultTreeCellRenderer();
        dateIconRenderer.setLeafIcon(new ImageIcon(getClass().getResource("/images/date.png")));
        dateIconRenderer.setOpenIcon(new ImageIcon(getClass().getResource("/images/date.png")));
        dateIconRenderer.setClosedIcon(new ImageIcon(getClass().getResource("/images/date.png")));

    }

    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {

        // Se pone el icono adecuado
        String typeOfvalue = "";
        typeOfvalue = value.getClass().toString();


        if (typeOfvalue.indexOf("project") != -1) {
            return databaseIconRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
        }

        if (typeOfvalue.indexOf("table") != -1) {
            return columnIconRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
        }

        if (typeOfvalue.indexOf("field") != -1) {
            field campito = (field) value;
            if (campito.getisKey()) {
                return primaryKeyIconRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
            } else {
                if (campito.getisForeignKey()) {
                    return foreignKeyIconRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
                } else {
                    switch(campito.getType())
                    {
                        case _date:
                                return dateIconRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
                       default:
                            return columnIconRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
                    }
                    
                }

            }

        }


//   if (leaf)
//   {
//      return columnIconRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
//   }
//   else if (expanded)
//   {
//      return tableIconRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
//   }
//   else
//   {// icono de database
//      return tableIconRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
//   }




        return null;

    }
}
